<template>
	<view>
		<view class="top-back">
			<view class="top-name">消息通知</view>
		</view>
		<view class="uni-container">
			<view class="query-back">
				<view class="back-view">
					<uni-forms ref="baseForm">
						<uni-forms-item label="接收案件待审核通知的人员：" labelWidth="200" labelAlign="right">
							<uni-easyinput type="textarea" autoHeight v-model="auditItme.value" placeholder="请输入手机号" />
						</uni-forms-item>
						<uni-forms-item label="接收案件待分配通知的人员：" labelWidth="200" labelAlign="right">
							<uni-easyinput type="textarea" autoHeight v-model="dispatchItem.value"
								placeholder="请输入手机号" />
						</uni-forms-item>
						<uni-forms-item label="接收用户留言通知的人员：" labelWidth="200" labelAlign="right">
							<uni-easyinput type="textarea" autoHeight v-model="leaveItem.value" placeholder="请输入手机号" />
						</uni-forms-item>
					</uni-forms>
				</view>
				<view class="bottom-btn" @click="submitData">保存</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				auditCode: "receive_case_audit_msg_mobile",
				dispatchCode: "receive_case_dispatch_msg_mobile",
				leaveCode: "receive_leave_msg_mobile",
				auditItme: {
					value: ''
				},
				dispatchItem: {
					value: ''
				},
				leaveItem: {
					value: ''
				}

			}

		},
		mounted() {
			this.requestData()
		},
		methods: {


			//网络请求示例
			async requestData() {
				let _this = this
				let parmas = {
					type: '1'
				};
				var url = "/comm/config/list";
				const res = await this.$myRequest({
					url: url,
					method: 'POST',
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					for (var i = 0; i < res.data.data.list.length; i++) {
						var item = res.data.data.list[i];
						if (item.code == _this.auditCode) {
							_this.auditItme = item;
						}
						if (item.code == _this.dispatchCode) {
							_this.dispatchItem = item;
						}
						if (item.code == _this.leaveCode) {
							_this.leaveItem = item;
						}
					}
				}
			},
			//保存
			async submitData() {

				var reg = /^1[3-9]\d{9}$/ //判断手机号
				if (!this.auditItme.value) {
					uni.showToast({
						title: '请填写接收案件待审核通知的人员',
						icon: 'none'
					})
					return;
				}
				if (!this.dispatchItem.value) {
					uni.showToast({
						title: '请填写接收案件待分配通知的人员',
						icon: 'none'
					})
					return;
				}
				if (!this.leaveItem.value) {
					uni.showToast({
						title: '请填写接收用户留言通知的人员',
						icon: 'none'
					})
					return;
				}
				var allTrue = true;

				var str = this.auditItme.value;
				var arr = str.split(',')
				arr.forEach((item, index) => {
					if (!reg.test(item)) {
						allTrue = false;
					}
				});
				var str1 = this.dispatchItem.value;
				var arr1 = str1.split(',')
				arr1.forEach((item, index) => {
					if (!reg.test(item)) {
						allTrue = false;
					}
				});
				var str2 = this.leaveItem.value;
				var arr2 = str1.split(',')
				arr2.forEach((item, index) => {
					if (!reg.test(item)) {
						allTrue = false;
					}
				});

				if (!allTrue) {
					uni.showToast({
						title: '请检查手机号格式是否正确',
						icon: 'none'
					})
					return;
				}

				let _this = this
				var parms = {
					'id': this.auditItme.id,
					'name': this.auditItme.name,
					'value': this.auditItme.value,
				}
				const res = await this.$myRequest({
					url: "/comm/config",
					method: 'PUT',
					data: parms
				})
				if (res.data.code == 200) {
					var parms1 = {
						'id': _this.dispatchItem.id,
						'name': _this.dispatchItem.name,
						'value': _this.dispatchItem.value,
					}
					const res1 = await this.$myRequest({
						url: "/comm/config",
						method: 'PUT',
						data: parms1
					})
					if (res1.data.code == 200) {
						var parms2 = {
							'id': _this.leaveItem.id,
							'name': _this.leaveItem.name,
							'value': _this.leaveItem.value,
						}
						const res2 = await this.$myRequest({
							url: "/comm/config",
							method: 'PUT',
							data: parms2
						})
						if (res2.data.code == 200) {
							uni.showToast({
								title: '保存成功'
							})
						}
					}
				}
			},

		}
	}
</script>

<style scoped>
	.top-back {
		display: flex;
		align-items: center;
		padding: 10px;
	}

	.top-name {
		color: #333;
		font-size: 18px;
		font-weight: bold;
		margin-left: 30px;
	}

	.uni-container {
		margin-left: 40px;
		margin-right: 16px;
		width: 90%;
	}

	.search-back {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}

	.back-view {
		width: 600px;
	}

	.query-back {
		width: 100%;
		margin-top: 15px;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
